package com.bus.controller;

import com.bus.entity.Provider;
import com.bus.service.ProviderService;
import com.bus.vo.ProviderVo;
import com.common.DataGridView;
import com.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bus/provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;

    @RequestMapping("/loadProvider")
    @ResponseBody
    public DataGridView loadProvider(ProviderVo providerVo) {
        Map<String, Object> map = providerService.selectAllProvider(providerVo);
        List<Provider> providers = (List<Provider>) map.get("providers");
        long total = (long) map.get("total");
        return new DataGridView(total, providers);
        /*QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        String customerName = customerVo.getCustomerName();
        String telephone = customerVo.getTelephone();
        String connectionPerson = customerVo.getConnectionPerson();
        queryWarpper.eq("available", SysConstast.AVAILABLE)
                .like(StringUtils.isNotBlank(customerName),"customerName",customerName)
                .like(StringUtils.isNotBlank(telephone),"telephone",telephone)
                .like(StringUtils.isNotBlank(connectionPerson),"connectionPerson",connectionPerson);
        IPage<Customer> page = new Page<>(customerVo.getPage(),customerVo.getLimit());
        IPage<Customer> result = customerService.page(page, queryWarpper);
        return new DataGridView(result.getTotal(),result.getRecords());*/
    }

    /**
     * 重定向到客户管理页面
     */
    @RequestMapping(value = "/providerManagement", method = RequestMethod.GET)
    public String showProvider() {
        return "redirect:/view/data/providerManager.jsp";
    }

    @RequestMapping("/addProvider")
    @ResponseBody
    public ResultObj addProvider(ProviderVo providerVo){
        try {
            providerService.addProvider(providerVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping("/updateProvider")
    @ResponseBody
    public ResultObj updateProvider(ProviderVo providerVo){
        try {
            providerService.updateProvider(providerVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }

    @RequestMapping("/deleteProvider")
    @ResponseBody
    public ResultObj deleteProvider(ProviderVo providerVo){
        try {
            providerService.deleteProvider(providerVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }

    @RequestMapping("/getProvider")
    @ResponseBody
    public List<Provider> getProvider(){
        return providerService.selectAll();
    }
}
